//
//  MYXsignatureViewController.m
//  meiyinxiang
//
//  Created by 陈李飞 on 2017/11/6.
//  Copyright © 2017年 hbjApple. All rights reserved.
//签名

#import "MYXsignatureViewController.h"
@interface MYXsignatureViewController ()<UITextViewDelegate,UIGestureRecognizerDelegate>
@property(strong,nonatomic)UITextView *feedbackContent;
@property(strong,nonatomic)UILabel *feedbackContentPlaceHolder;
@property(strong,nonatomic)UILabel *countTextNumber;

@property(assign,nonatomic)float contentViewHeight;
@property(assign,nonatomic)float offsetY;

@property(assign,nonatomic)BOOL isPostFeedBack;

@property(nonatomic, strong) MYXHttpManager *MYXHttpManag;
@end

@implementation MYXsignatureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _isPostFeedBack = NO;//初始化变量值
    self.MYXHttpManag =[[MYXHttpManager alloc]init];
    [self getContentHeight];
    [self initUI];
   
}

-(void)right
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"确定" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(sure) forControlEvents:UIControlEventTouchUpInside];
    btn.frame = CGRectMake(0, 0, 60, 44);
    UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    [self.navigationItem setRightBarButtonItem:cancelItem animated:NO];
    
}
-(void)sure{
    NSString *stringText = [_feedbackContent.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if (stringText.length==0){
        [self showNoReceivertext:@"请说点什么"];
        return;
    }
    //为了防止键盘的闪屏，延迟0.25秒退出
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self.signatureBlock) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"info" object:nil];
            self.signatureBlock(stringText);
        }
        [self.navigationController popViewControllerAnimated:YES];
        
    });

}
-(void)getContentHeight
{
    //根据屏幕的尺寸，确定文本框的高度
    if ([[UIScreen mainScreen] bounds].size.height == 480) {
        _contentViewHeight = 100;
    }else if([[UIScreen mainScreen] bounds].size.height == 568){
        _contentViewHeight = 150;
    }else if([[UIScreen mainScreen] bounds].size.height == 667){
        _contentViewHeight = 210;
    }else{
        _contentViewHeight = 220;
    }
}
-(void)initUI
{
    self.view.backgroundColor = [UIColor colorWithRed:238/255.0 green:238/255.0 blue:238/255.0 alpha:1.0];
    _feedbackContent = [UITextView new];
    _feedbackContent.delegate = self;
    _feedbackContent.backgroundColor = [UIColor whiteColor];
    _feedbackContent.textColor = [UIColor blackColor];
    _feedbackContent.font = [UIFont systemFontOfSize:16];
    _feedbackContent.text = self.signature;
    _feedbackContent.textContainerInset = UIEdgeInsetsMake(18, 14, 0, 14);
    
    [self.view addSubview:_feedbackContent];
    
    [_feedbackContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(self.view).with.offset(0);
        make.width.equalTo(self.view);
        make.height.equalTo(@(_contentViewHeight));
    }];
    
    _feedbackContentPlaceHolder = [UILabel new];
    _feedbackContentPlaceHolder.numberOfLines = 2;
    _feedbackContentPlaceHolder.textColor = [UIColor groupTableViewBackgroundColor];
    if (self.signature.length>0) {
        
    }else{
        _feedbackContentPlaceHolder.text = self.placehoderString;
    }
    
    _feedbackContentPlaceHolder.font = [UIFont systemFontOfSize:16];
    //_feedbackContentPlaceHolder.textColor = [UIColor colorWithRed:183/255.0 green:183/255.0 blue:183/255.0 alpha:1.0];
    [self.view addSubview:_feedbackContentPlaceHolder];
    
    [_feedbackContentPlaceHolder mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(15);
        make.right.equalTo(self.view).with.offset(-15);
        make.top.equalTo(self.view).with.offset(18);
    }];
    //反馈内容字符限制
    _countTextNumber = [UILabel new];
    _countTextNumber.text = @"1/100";
    _countTextNumber.textColor = MYYtitleColor;
    [self.view addSubview:_countTextNumber];
    
    [_countTextNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_feedbackContent.mas_right).with.offset(-5);
        make.top.equalTo(_feedbackContent.mas_bottom).with.offset(5);
    }];
    
    [self right];
    //单独处理问题反馈功能
    if ([self.navigationItem.title isEqualToString:@"个人签名"]){
        self.companyIntrLabel.text = @"";
        
    }
   
    
}
#pragma TextViewDelegate
//监听是否显示默认文字
-(void)textViewDidChange:(UITextView *)textView
{
    if ([_feedbackContent.text length]==0) {
        [_feedbackContentPlaceHolder setHidden:NO];
        
        _countTextNumber.text = @"1/100";
    }else{
        [_feedbackContentPlaceHolder setHidden:YES];
        //如果粘贴了文本后，长度超过200，则截断超出部分
        if (_feedbackContent.text.length>199) {
            _feedbackContent.text = [_feedbackContent.text substringWithRange:NSMakeRange(0, 99)];
            _countTextNumber.text = @"100/100";
        }else{
            //修改剩余字符数量的label
            _countTextNumber.text = [NSString stringWithFormat:@"%d/100",(int)(100-_feedbackContent.text.length)];
        }
    }
}

//限制字符长度
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([_feedbackContent.text length]>100) {
        if (![text isEqualToString:@""]) {
            return NO;
        }
    }
    
    return YES;
}

#pragma mark - 没有输入任何内容的提示
-(void)showNoReceivertext:(NSString *)message
{
    [JXTHUDHelper showHUDText:message type:@"warning" inView:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
